Skip to content

Support voltage formulas#7

Merged
shsms merged 14 commits into
frequenz-floss:v0.x.xfrom
shsms:voltage-formulas
Jul 28, 2025
Merged

Support voltage formulas#7
shsms merged 14 commits into
frequenz-floss:v0.x.xfrom
shsms:voltage-formulas

Conversation

@shsms
Copy link
Copy Markdown
Collaborator

@shsms shsms commented Jul 17, 2025

This is also a major internal revamp of how metrics are represented and how the component graph is accessed, but the external interface only has minor changes.

@shsms shsms added the cmd:skip-release-notes It is not necessary to update release notes for this PR label Jul 17, 2025
@shsms
Copy link
Copy Markdown
Collaborator Author

shsms commented Jul 17, 2025

Draft because it depends on unmerged code from the component graph repo.

@shsms shsms requested a review from niklas-timpe July 17, 2025 09:46
@shsms
Copy link
Copy Markdown
Collaborator Author

shsms commented Jul 17, 2025

And based on #6

@shsms shsms force-pushed the voltage-formulas branch from cae9d74 to b95df67 Compare July 18, 2025 10:01
@shsms shsms marked this pull request as ready for review July 18, 2025 10:02
Copilot AI review requested due to automatic review settings July 18, 2025 10:02
@shsms
Copy link
Copy Markdown
Collaborator Author

shsms commented Jul 18, 2025

Ready for review now.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for voltage formulas by implementing a major internal refactoring of the metric representation and component graph access patterns. The external interface maintains compatibility with only minor changes, while internally introducing a trait-based approach that differentiates between aggregation and coalesce formulas based on metric types.

  • Refactored metrics from enum to trait-based system with AcMetric trait and individual metric structs
  • Introduced separate AggregationFormula and CoalesceFormula types with voltage metrics using coalesce behavior
  • Updated logical meter actor to track formulas and resamplers by both formula string and metric type

Reviewed Changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/logical_meter/metric/metric_trait.rs Defines new AcMetric trait for type-safe metric handling
src/logical_meter/metric.rs Converts enum-based metrics to trait-based system using macro generation
src/logical_meter/logical_meter_handle.rs Updates public API to use generic methods with trait bounds
src/logical_meter/logical_meter_actor.rs Modifies internal tracking to use composite keys of (formula, metric)
src/logical_meter/formula/graph_formula_provider.rs Implements trait for generating formulas from component graph
src/logical_meter/formula/coalesce_formula.rs New formula type for voltage metrics using coalesce operations
src/logical_meter/formula/aggregation_formula.rs Extracted aggregation formula with arithmetic operations
src/logical_meter/formula.rs Refactored to trait-based formula system
src/logical_meter.rs Updated module exports
src/lib.rs Updated public API exports
examples/logical_meter.rs Updated example to use new metric syntax
Cargo.toml Updated component graph dependency version

use crate::{Error, Metric, Sample};
use crate::{Error, Sample};
use tokio::sync::broadcast;

Copy link

Copilot AI Jul 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The trait method uses impl Future return type which requires importing std::future::Future. This import is missing and will cause compilation errors.

Suggested change
use std::future::Future;

Copilot uses AI. Check for mistakes.
macro_rules! graph_formula_provider {
($(($fnname:ident $(, $idsparam:ident)?)),+ $(,)?) => {$(

fn $fnname<M: crate::metric::metric_trait::AcMetric>(
Copy link

Copilot AI Jul 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The macro uses an absolute path crate::metric::metric_trait::AcMetric but the relative import path should be used consistently with other parts of the file. Consider using crate::logical_meter::metric::metric_trait::AcMetric or a relative import.

Suggested change
fn $fnname<M: crate::metric::metric_trait::AcMetric>(
fn $fnname<M: super::metric::metric_trait::AcMetric>(

Copilot uses AI. Check for mistakes.
macro_rules! impl_graph_formula_provider {
($(($fnname:ident, $graphfnname:ident$(, $idsparam:ident)?)),+ $(,)?) => {$(

fn $fnname<M: crate::metric::metric_trait::AcMetric>(
Copy link

Copilot AI Jul 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue as above - the macro uses an inconsistent import path crate::metric::metric_trait::AcMetric instead of the correct module path.

Suggested change
fn $fnname<M: crate::metric::metric_trait::AcMetric>(
fn $fnname<M: AcMetric>(

Copilot uses AI. Check for mistakes.
niklas-timpe
niklas-timpe previously approved these changes Jul 18, 2025
Comment thread Cargo.toml Outdated
[dependencies]
chrono = "0.4"
frequenz-microgrid-component-graph = { git = "https://github.com/frequenz-floss/frequenz-microgrid-component-graph-rs.git", rev = "ab3b998" }
# frequenz-microgrid-component-graph = { git = "https://github.com/shsms/frequenz-microgrid-component-graph-rs.git", branch = "coalesce-formulas" }
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should that stay here?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, no

Comment thread src/logical_meter/metric.rs
Comment thread src/logical_meter/logical_meter_handle.rs
shsms added 13 commits July 18, 2025 14:20
Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
Because there will be another type of formula that doesn't support
aggregation.

Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
This will be implemented by the upcoming `CoalesceFormula` as well.

Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
This also enables us to delegate the component graph formula creation
to the logical meter formulas, through the `GraphFormulaProvider`
trait.

Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
And introduce support for voltage and frequency metrics using
`CoalesceFormula`s.

Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
This is a bugfix, because earlier there was only one resampler for
each component and data from all metrics were coming from that one
resampler, which had the data only for the first metric that was
subscribed to.

Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
The channel used to communicate the status of a component data stream
was called `stream_stopped_tx/rx`, which is no longer accurate.

Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
Earlier we were picking a random metric and sending its data to all
formulas, which was incorrect.

Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
@shsms shsms force-pushed the voltage-formulas branch from 6cb1337 to 9aa1c78 Compare July 28, 2025 08:00
Copy link
Copy Markdown
Collaborator

@niklas-timpe niklas-timpe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@shsms shsms added this pull request to the merge queue Jul 28, 2025
Merged via the queue into frequenz-floss:v0.x.x with commit 3476bfe Jul 28, 2025
3 checks passed
@shsms shsms deleted the voltage-formulas branch July 28, 2025 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cmd:skip-release-notes It is not necessary to update release notes for this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants